package myQueue;

class LinkNode<T>        //链队列结点泛型类
{
    T data;
    LinkNode<T> next;

    public LinkNode()        //构造方法
    {
        next = null;
    }

    public LinkNode(T d)    //重载构造方法
    {
        data = d;
        next = null;
    }
}

public class MyLinkedQueue<T> {
    LinkNode<T> front;            //首结点指针
    LinkNode<T> rear;            //尾结点指针

    public MyLinkedQueue()        //构造方法
    {
        front = null;
        rear = null;
    }

    //队列的基本运算算法
    public boolean empty()    //判断队列是否为空
    {
        return front == null;
    }

    public void push(T e)                  //元素e进队
    {
        LinkNode<T> s = new LinkNode<T>(e);      //新建结点s
        if (empty())              //原链队为空
            front = rear = s;
        else                         //原链队不空
        {
            rear.next = s;                 //将s结点链接到rear后面
            rear = s;
        }
    }

    public T pop()                //出队操作
    {
        T e;
        if (empty())            //原链队不空
            throw new IllegalArgumentException("队空");
        if (front == rear)            //原链队只有一个结点
        {
            e = (T) front.data;        //取首结点值
            front = rear = null;        //置为空
        } else                    //原链队有多个结点
        {
            e = (T) front.data;        //取首结点值
            front = front.next;        //front指向下一个结点
        }
        return e;
    }

    public T peek()            //取队顶元素操作
    {
        if (empty())
            throw new IllegalArgumentException("队空");
        T e = (T) front.data;            //取首结点值
        return e;
    }


}
